﻿Public Class CommonMethods
    Public Shared Function Encrypt(ByRef pPassPhrase As String, ByVal pTextToEncrypt As String) As String
        If pPassPhrase.Length > 16 Then
            'limitation of the encryption mechanism
            pPassPhrase = pPassPhrase.Substring(0, 16)
        End If

        If pTextToEncrypt.Trim.Length = 0 Then
            'the Text to encrypt not set!!!
            Return String.Empty
        End If

        Dim skey As New Encryption.Data(pPassPhrase)
        Dim sym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
        Dim objEncryptedData As Encryption.Data
        objEncryptedData = sym.Encrypt(New Encryption.Data(pTextToEncrypt), skey)
        Return objEncryptedData.ToHex
    End Function

    Public Shared Function Decrypt(ByRef pPassPhrase As String, ByVal pHexStream As String) As String
        Try
            ''pHexStream = pHexStream.Replace("-", "")
            'Dim enc As System.Text.Encoding = System.Text.Encoding.ASCII
            'Dim myByteArray As Byte() = Convert.FromBase64String(pHexStream)
            'pHexStream = enc.GetString(myByteArray)

            Dim objSym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
            Dim encryptedData As New Encryption.Data
            encryptedData.Hex = pHexStream
            Dim decryptedData As Encryption.Data
            decryptedData = objSym.Decrypt(encryptedData, New Encryption.Data(pPassPhrase))
            Return decryptedData.Text
        Catch
            Return Nothing
        End Try
    End Function
End Class
